package com.jh3502.helper.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jh3502.helper.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Harisucici
 */
public class ActionSheetDialog {
    private Point point;
    private Context context;
    private Dialog dialog;
    private TextView txtTitle;
    private TextView txtCancel;
    private LinearLayout lLayoutContent;
    private ScrollView sLayoutContent;
    private boolean showTitle = false;
    private List<SheetItem> sheetItemList;
    private Display display;

    public ActionSheetDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        point = new Point();
        display.getSize(point);
    }

    @NonNull
    public ActionSheetDialog builder() {
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_actionsheet, null);

        view.setMinimumWidth(point.x);
        sLayoutContent = view.findViewById(R.id.sLayout_content);
        lLayoutContent = view.findViewById(R.id.lLayout_content);
        txtTitle = view.findViewById(R.id.txt_title);
        txtCancel = view.findViewById(R.id.txt_cancel);
        txtCancel.setOnClickListener(v -> dialog.dismiss());

        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    @NonNull
    public ActionSheetDialog setTitle(String title) {
        showTitle = true;
        txtTitle.setVisibility(View.VISIBLE);
        txtTitle.setText(title);
        return this;
    }

    @NonNull
    public ActionSheetDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    @NonNull
    public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    @NonNull
    public ActionSheetDialog addSheetItem(String strItem, int colorId,
                                          OnSheetItemClickListener listener) {
        if (sheetItemList == null) {
            sheetItemList = new ArrayList<>();
        }
        sheetItemList.add(new SheetItem(strItem, colorId, listener));
        return this;
    }

    @SuppressWarnings("UnusedAssignment")
    private void setSheetItems() {
        if (sheetItemList == null || sheetItemList.size() <= 0) {
            return;
        }

        int size = sheetItemList.size();

        if (size >= 7) {
            LayoutParams params = (LayoutParams) sLayoutContent.getLayoutParams();
            params.height = point.y / 2;
            sLayoutContent.setLayoutParams(params);
        }

        for (int i = 1; i <= size; i++) {
            SheetItem sheetItem = sheetItemList.get(i - 1);
            String strItem = sheetItem.name;
            int colorId = sheetItem.colorId;
            final OnSheetItemClickListener listener = sheetItem.itemClickListener;

            TextView textView = new TextView(context);
            textView.setText(strItem);
            textView.setTextSize(context.getResources().getDimension(R.dimen.item_text_size_little));
            textView.setGravity(Gravity.CENTER);


            if (size == 1) {
                if (showTitle) {
                    textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                } else {
                    textView.setBackgroundResource(R.drawable.actionsheet_single_selector);
                }
            } else {
                if (showTitle) {
                    if (i >= 1 && i < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                } else {
                    if (i == 1) {
                        textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
                    } else if (i < size) {
                        textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
                    } else {
                        textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
                    }
                }
            }

            textView.setTextColor(ContextCompat.getColor(context, colorId));

            float scale = context.getResources().getDisplayMetrics().density;
            int height = (int) (75 * scale + 0.5f);
            textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height));
//            textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, txtCancel.getHeight()));

            textView.setOnClickListener(v -> {
                listener.onClick();
                dialog.dismiss();
            });

            lLayoutContent.addView(textView);
        }
    }

    public void show() {
        setSheetItems();
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public enum SheetItemColor {
        Blue("#3e85c4");

        private String name;

        SheetItemColor(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public interface OnSheetItemClickListener {
        void onClick();
    }

    public class SheetItem {
        String name;
        OnSheetItemClickListener itemClickListener;
        int colorId;

        public SheetItem(String name, int colorId,
                         OnSheetItemClickListener itemClickListener) {
            this.name = name;
            this.colorId = colorId;
            this.itemClickListener = itemClickListener;
        }
    }

	/*
     * case R.id.btn1:
			new ActionSheetDialog(ChoosePerson.this)
					.builder()
					.setTitle("清空消息列表后，聊天记录依然保留，确定要清空消息列表？")
					.setCancelable(false)
					.setCanceledOnTouchOutside(false)
					.addSheetItem("清空消息列表", SheetItemColor.Red,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							}).show();
			break;
		case R.id.btn2:
			new ActionSheetDialog(ChoosePerson.this)
					.builder()
					.setCancelable(false)
					.setCanceledOnTouchOutside(false)
					.addSheetItem("发送给好友", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							})
					.addSheetItem("转载到空间相册", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							})
					.addSheetItem("上传到群相册", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							})
					.addSheetItem("保存到手机", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							})
					.addSheetItem("收藏", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							})
					.addSheetItem("查看聊天图片", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {

								}
							}).show();
			break;
		case R.id.btn3:
			new ActionSheetDialog(ChoosePerson.this)
					.builder()
					.setTitle("请选择操作")
					.setCancelable(false)
					.setCanceledOnTouchOutside(false)
					.addSheetItem("条目一", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目二", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目三", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目四", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目五", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目六", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目七", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目八", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目九", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							})
					.addSheetItem("条目十", SheetItemColor.Blue,
							new OnSheetItemClickListener() {
								@Override
								public void onClick(int which) {
									Toast.makeText(ChoosePerson.this,
											"item" + which, Toast.LENGTH_SHORT)
											.show();
								}
							}).show();
			break;
		case R.id.btn4:
			new AlertDialog(ChoosePerson.this).builder().setTitle("退出当前账号")
					.setMsg("再连续登录15天，就可变身为QQ达人。退出QQ可能会使你现有记录归零，确定退出？")
					.setPositiveButton("确认退出", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					}).setNegativeButton("取消", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					}).show();
			break;
		case R.id.btn5:
			new AlertDialog(ChoosePerson.this).builder()
					.setMsg("你现在无法接收到新消息提醒。请到系统-设置-通知中开启消息提醒")
					.setNegativeButton("确定", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					}).show();
			break;
		default:
			break;
		}
	 * */
}
